package com.greenline.push.service.core.fetch;

import java.util.concurrent.TimeUnit;

import javax.annotation.Resource;

import org.apache.log4j.Logger;

import com.greenline.push.core.enums.Tasks;
import com.greenline.push.core.pushcenter.TaskSyncManager;
import com.greenline.push.dal.pushcenter.dataobject.TaskSyncDO;
import com.greenline.push.service.core.ProcessQueueList;
import com.greenline.push.service.core.utils.Constants;

public abstract class DeaultAsyncFetcher implements IAsyncFetcher {

    @Resource
    private TaskSyncManager taskSyncManager;

    private final Logger logger = Constants.LOG_SCHEDULE;

    @Override
    public void fetch(ProcessQueueList processQueueList) {
        logger.info(String.format("task:%s,开始启动.======", getTask()));
        Tasks task = getTask();
        startTask(processQueueList, task);
        logger.info(String.format("task:%s,启动完成.======", task));
    }

    protected abstract Tasks getTask();

    protected abstract void startTask(ProcessQueueList processQueueList, Tasks task);

    /**
     * get timeUnit
     * 
     * @return
     */
    protected TimeUnit getTimeUnit() {
        return TimeUnit.SECONDS;
    }

    /**
     * get runPeriod
     * 
     * @return
     */
    protected Integer getRunPeriod() {
        TaskSyncDO taskFetchRealTime = taskSyncManager.getTaskSyncByType(getTask().getType());
        return taskFetchRealTime.getRunPeriod();
    }
}
